<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserSocialite extends Model
{
    protected $guarded = ['id'];

    protected $casts = [
        'auth_info' => 'array',
    ];

    public function user ()
    {
        return $this->belongsTo('App\User');
    }

    public static function getByDriverAndOpenId($driver, $openId)
    {
        return self::where([
            'driver' => $driver,
            'open_id' => $openId
        ])->first();
    }

    public static function createOne($userId, $driver, $openId, $accessToken = NULL, $refreshToken = NULL, $authInfo=NULL)
    {
        return self::create([
            'user_id' => $userId,
            'driver' => $driver,
            'open_id' => $openId,
            'access_token' => $accessToken,
            'refresh_token' => $refreshToken,
            'auth_info' => $authInfo
        ]);
    }
}
